<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>浏览页面</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css"/>
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css"/>
</head>
<body class="pear-container">

<!-- 顶部查询表单 -->
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form top-search-from">

            <div class="layui-form-item">
                <label class="layui-form-label">学号</label>
                <div class="layui-input-block">
                    <input type="hidden" name="uid[]" value="like">
                    <input type="text" name="uid[]" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-block">
                    <input type="hidden" name="name[]" value="like">
                    <input type="text" name="name[]" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <select name="sex" lay-verify="" lay-search>
                        <option value="">请选择性别</option>
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </div>

            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-block">
                    <input type="hidden" name="sysid[]" value="like" class="layui-input">
                    <input type="text" name="sysid[]" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">出生年月</label>
                <div class="layui-input-block">
                    <div class="layui-input-block" id="birth">
                        <input type="text" autocomplete="off" name="birth[]" id="birth-date-start"
                               class="layui-input inline-block" placeholder="开始时间">
                        -
                        <input type="text" autocomplete="off" name="birth[]" id="birth-date-end"
                               class="layui-input inline-block" placeholder="结束时间">
                    </div>
                </div>
            </div>

            <div class="layui-form-item layui-inline">
                <label class="layui-form-label"></label>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                    <i class="layui-icon layui-icon-search"></i>查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                    <i class="layui-icon layui-icon-refresh"></i>重置
                </button>
            </div>
            <div class="toggle-btn">
                <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
            </div>
        </form>
    </div>
</div>

<!-- 数据表格 -->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="data-table" lay-filter="data-table"></table>
    </div>
</div>

<!-- 表格顶部工具栏 -->
<script type="text/html" id="table-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" permission="student.insert">
        <i class="layui-icon layui-icon-add-1"></i>新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" permission="student.delete">
        <i class="layui-icon layui-icon-delete"></i>删除
    </button>

    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="syncAccount" permission="student.syncAccount" loading>
        <i class="layui-icon layui-icon-vercode"></i>同步到账户
    </button>
</script>

<!-- 表格行工具栏 -->
<script type="text/html" id="table-bar">
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="student.update">编辑</button>
    <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="student.delete">删除</button>
</script>

<script src="/app/admin/component/layui/layui.js"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script src="/app/admin/admin/js/common.js"></script>

<script>

    // 相关常量
    const PRIMARY_KEY = "id";
    const SELECT_API = "/student/select";
    const UPDATE_API = "/student/update";
    const DELETE_API = "/student/delete";
    const INSERT_URL = "/student/insert";
    const UPDATE_URL = "/student/update";
    const SYNC_URL = "/student/syncAccount";

    // 字段 出生年月 birth
    layui.use(["laydate"], function () {
        layui.laydate.render({
            elem: "#birth",
            range: ["#birth-date-start", "#birth-date-end"],
            type: "date",
        });
    })

    // 表格渲染
    layui.use(["table", "form", "common", "popup", "util","button"], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.$;
        let button = layui.button;
        let common = layui.common;
        let util = layui.util;

        // 表头参数
        let cols = [
            {
                type: "checkbox",
                align: "center"
            }, {
                title: "主键",
                field: "id",
                align: "center"
            }, {
                title: "学号",
                field: "uid",
                sort: true,
                align: "center"
            }, {
                title: "姓名",
                field: "name",
                sort: true, align: "center"
            }, {
                title: "性别",
                field: "sex", align: "center"
            }, {
                title: "电话",
                field: "phone",
                hide: true, align: "center"
            }, {
                title: "身份证号",
                field: "sysid",
                hide: true, align: "center"
            }, {
                title: "出生年月",
                field: "birth", align: "center"
            }, {
                title: "民族",
                field: "minzu", align: "center"
            }, {
                title: "籍贯",
                field: "jingguan", align: "center"
            }, {
                title: "户籍",
                field: "hukou", align: "center",
                hide: true,
            }, {
                title: "户籍地址",
                field: "huji",
                hide: true, align: "center"
            }, {
                title: "现住址",
                field: "xianzz",
                hide: true, align: "center"
            }, {
                title: "是否留守儿童",
                field: "liushou", align: "center",
                hide: true,
            }, {
                title: "毕业学校",
                field: "biye",
                hide: true, align: "center"
            }, {
                title: "获奖情况",
                field: "huojiang",
                hide: true, align: "center"
            }, {
                title: "创建时间",
                field: "created_at",
                hide: true, align: "center"
            }, {
                title: "更新时间",
                field: "updated_at",
                hide: true, align: "center"
            }, {
                title: "操作",
                toolbar: "#table-bar",
                align: "center",
                fixed: "right",
                width: 120,
            }
        ];

        // 渲染表格
        table.render({
            elem: "#data-table",
            url: SELECT_API,
            page: true,
            cols: [cols],
            skin: "line",
            size: "lg",
            toolbar: "#table-toolbar",
            autoSort: false,
            defaultToolbar: [{
                title: "刷新",
                layEvent: "refresh",
                icon: "layui-icon-refresh",
            }, "filter", "print", "exports"],
            done: function () {
                layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
            }
        });


        // 编辑或删除行事件
        table.on("tool(data-table)", function (obj) {
            if (obj.event === "remove") {
                remove(obj);
            } else if (obj.event === "edit") {
                edit(obj);
            }
        });

        // 表格顶部工具栏事件
        table.on("toolbar(data-table)", function (obj) {
            if (obj.event === "add") {
                add();
            } else if (obj.event === "refresh") {
                refreshTable();
            } else if (obj.event === "batchRemove") {
                batchRemove(obj);
            } else if (obj.event === 'syncAccount') {
                syncAccount(obj);
            }
        });

        // 表格顶部搜索事件
        form.on("submit(table-query)", function (data) {
            table.reload("data-table", {
                where: data.field
            })
            return false;
        });

        // 表格顶部搜索重置事件
        form.on("submit(table-reset)", function (data) {
            table.reload("data-table", {
                where: []
            })
        });

        // 表格排序事件
        table.on("sort(data-table)", function (obj) {
            table.reload("data-table", {
                initSort: obj,
                scrollPos: "fixed",
                where: {
                    field: obj.field,
                    order: obj.type
                }
            });
        });

        // 表格新增数据
        let add = function () {
            layer.open({
                type: 2,
                title: "新增",
                shade: 0.1,
                shadeClose: true,
                area: [common.isModile() ? "100%" : "500px", common.isModile() ? "100%" : "450px"],
                content: INSERT_URL
            });
        }

        // 表格编辑数据
        let edit = function (obj) {
            let value = obj.data[PRIMARY_KEY];
            layer.open({
                type: 2,
                title: "修改",
                shade: 0.1,
                shadeClose: true,
                area: [common.isModile() ? "100%" : "500px", common.isModile() ? "100%" : "450px"],
                content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
            });
        }

        // 删除一行
        let remove = function (obj) {
            return doRemove(obj.data[PRIMARY_KEY]);
        }

        // 删除多行
        let batchRemove = function (obj) {
            let checkIds = common.checkField(obj, PRIMARY_KEY);
            if (checkIds === "") {
                layui.popup.warning("未选中数据");
                return false;
            }
            doRemove(checkIds.split(","));
        }

        // 执行删除
        let doRemove = function (ids) {
            let data = {};
            data[PRIMARY_KEY] = ids;
            layer.confirm("确定删除?", {
                icon: 3,
                title: "提示"
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DELETE_API,
                    data: data,
                    dataType: "json",
                    type: "post",
                    success: function (res) {
                        layer.close(loading);
                        if (res.code) {
                            return layui.popup.failure(res.msg);
                        }
                        return layui.popup.success("操作成功", refreshTable);
                    }
                })
            });
        }

        // 刷新表格数据
        window.refreshTable = function (param) {
            table.reloadData("data-table", {
                scrollPos: "fixed"
            });
        }

        let syncAccount = function (obj) {
            let dom = button.load({
                elem: '[loading]'
            })

            layui.$.post(SYNC_URL, function (res) {
                dom.stop(function() {
                    if (res.code) {
                        return layui.popup.failure(res.msg);
                    }
                    return layui.popup.success("同步成功", function (index) {
                        refreshTable();
                        layer.close(index)
                    });
                });
            })
        }
    })

</script>
</body>
</html>
